package com.ziyear.servlet;

import cn.hutool.core.util.StrUtil;
import com.ziyear.common.util.TrainUtils;
import com.ziyear.domain.Member;
import com.ziyear.domain.Pmember;
import com.ziyear.domain.Prep;
import com.ziyear.domain.Train;
import com.ziyear.service.PmemberService;
import com.ziyear.service.PrepService;
import com.ziyear.service.TrainService;
import com.ziyear.service.impl.PmemberServiceImpl;
import com.ziyear.service.impl.PrepServiceImpl;
import com.ziyear.service.impl.TrainServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

/**
 * 功能描述 : 会员  实现订票功能的Servlet
 *
 * @author Ziyear
 */
public class BookingServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         *  先执行
         */
        HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("member");

        Integer trainId = (Integer) request.getAttribute("trainId");
        String seatType = (String) request.getAttribute("seatType");
        boolean waiting = Boolean.parseBoolean(request.getParameter("waiting"));
        TrainService trainService = new TrainServiceImpl();
        Train train = trainService.getById(trainId);
        // 根据会员ID获取会员个人信息
        Integer memberId = member.getMemberId(); // 从Session中获取
        PmemberService pmemberService = new PmemberServiceImpl();
        Pmember pmember = pmemberService.getPmemberByMemberId(memberId);

        // 将数据转发到界面
        request.setAttribute("train", train);
        BigDecimal price = TrainUtils.getPriceBySeatType(train, seatType);
        request.setAttribute("price", price);
        request.setAttribute("pmember", pmember);
        request.setAttribute("seatType", seatType);
        request.setAttribute("waiting", waiting);
        request.setAttribute("seatCodeRadio", TrainUtils.buildSeatCodeRadio(seatType));
        request.getRequestDispatcher("pages/user/booking.jsp").forward(request, response);

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        /**
         * 确认后执行，
         * 将订票信息传入prep表
         * 将train表的本车次座位数量-1
         */
        Integer trainId = Integer.valueOf(request.getParameter("trainId"));

        String seatType = request.getParameter("seatType");
        String seatCode = request.getParameter("seatCode");
        boolean waiting = Boolean.parseBoolean(request.getParameter("waiting"));
        HttpSession session = request.getSession();
        boolean changeFlag = Boolean.parseBoolean(request.getParameter("changePrep"));


        // 根据会员ID获取会员个人信息
        Member member = (Member) request.getSession().getAttribute("member");    // 从Session中获取
        Integer memberId = member.getMemberId();
        ;
        PmemberService pmemberService = new PmemberServiceImpl();
        Pmember pmember = pmemberService.getPmemberByMemberId(memberId);

        PrepService prepService = new PrepServiceImpl();
        Prep prep = prepService.queryByTrainIdAndMemberId(trainId, memberId);
        if (!changeFlag) {
            if (prep != null) {
                out.write("<script>alert('很抱歉,订票失败,当前车次已有订单！');"
                        + "window.location.href='GetByStartEndStationServlet'</script>");
                out.close();
                return;
            }
        }

        boolean success = prepService.create(pmember.getPmemberId(), trainId, seatType, waiting, seatCode);

        prep = prepService.queryByTrainIdAndMemberId(trainId, memberId);
        // 是否抢到指定座位 true 需要展示没有抢到指定位置
        boolean seatCodeFlag = StrUtil.isNotBlank(seatCode) && !prep.getSeatCode().contains(seatCode);
        if (success) {
            if (changeFlag) {
                // 修改session，表示改签结束
                session.setAttribute("changePrep", "false");
                if (seatCodeFlag) {
                    out.write("<script>alert('改签成功！未抢到指定座位，请在我的订单进一步处理！');"
                            + "window.location.href='MyPrepServlet'</script>");
                } else {
                    out.write("<script>alert('改签成功！请在我的订单进一步处理！');"
                            + "window.location.href='MyPrepServlet'</script>");
                }
            }
            if (seatCodeFlag) {
                out.write("<script>alert('订票成功，未抢到指定座位，请在我的订单进一步处理！');"
                        + "window.location.href='GetByStartEndStationServlet'</script>");
            } else {
                out.write("<script>alert('订票成功！请在我的订单进一步处理！');"
                        + "window.location.href='GetByStartEndStationServlet'</script>");
            }
        } else {
            out.write("<script>alert('很抱歉,订票失败！');"
                    + "window.location.href='GetByStartEndStationServlet'</script>");
        }
        out.close();
    }

}
